package SellerAgent;

import Ontologies.Transaction;
import Ontologies.TreeWithPrice;
import com.google.gson.Gson;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import main.MainFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: jiaka_000
 * Date: 13-5-18
 * Time:
 * gui class to show the selling report
 * Edit by DuyHanPhan 23May: update runningBuyerList for the launcher GUI, and stop this seller when this SellingReportGUI closed
 */
public class SellingReportGUI extends JFrame {
    private static final int BORDER = 5;
    /**
     * the reference of seller agent
     */
    static Seller myAgent;

    /**
     * the reference of selling list,  from seller agent
     */
    private List<TreeWithPrice> sellingList;
    /**
     * the reference of transaction list,  from seller agent
     */
    private List<Transaction> transactionList;

    private JTable sellingTable;
    private JTable transactionTable;

    /**
     * constructor of report gui class
     * @param myAgent     seller agent
     */
    public SellingReportGUI(Seller myAgent) {
    	SellingReportGUI.myAgent = myAgent;
        sellingList=myAgent.getSellingList();
        transactionList = myAgent.getTransactionList();

        setTitle(myAgent.getLocalName()+ " Report");
        setResizable(false);
        
        //setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        //DuyHanPhan 23May not allow user close this window, has to use EXIT button
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
        setBounds(300,50,510,600);

        JPanel p=new JPanel(new GridLayout(2,1));

        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
        panel.add(new JLabel("Selling List"));

        sellingTable = new JTable(new SellingListTableModel(sellingList)){
            public boolean isCellEditable(int row, int column) {
                    return false;
            }};
        JScrollPane scrollPane= new JScrollPane(sellingTable);
        scrollPane.setSize(500, 100);
        panel.add(scrollPane);

        //getContentPane().add(panel,BorderLayout.NORTH);
        p.add(panel);

        panel = new JPanel();
        panel.setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
        panel.add(new JLabel("Transaction List"));

        transactionTable = new JTable(new TransactionTableModelSellerView(transactionList)){
            public boolean isCellEditable(int row, int column) {
                return false;
            }};
        scrollPane= new JScrollPane(transactionTable);
        scrollPane.setSize(500, 100);
        panel.add(scrollPane);
        p.add(panel);

        getContentPane().add(p,BorderLayout.CENTER);

        panel=new JPanel();
        JButton exitBtn= new JButton("Exit");
        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SellingReportGUI.this.setVisible(false);
                
              //TODO (DUY HAN PHAN) I have done the same for buyer!!!!!!!!!!!!!!!!!!!!!!!!!
				MainFrame.runningSellerList.remove(SellingReportGUI.myAgent.getLocalName());
                //modified by kai for inappropriate exit way
				//SellingReportGUI.myAgent.doDelete();
                SellingReportGUI.myAgent.doQuitAction("Exit "+  SellingReportGUI.myAgent.getLocalName());

            }
        });
        panel.add(exitBtn);
        getContentPane().add(panel,BorderLayout.SOUTH);


    }

    /**
     * update the tables view
     * @param sellingList  the updated selling list
     * @param transactionList  the updated transaction list
     */
    public void updateTables(List<TreeWithPrice> sellingList,List<Transaction> transactionList)
    {
        this.sellingList=sellingList;
        this.transactionList=transactionList;

        sellingTable.setModel(new SellingListTableModel(sellingList));
        sellingTable.updateUI();

        transactionTable.setModel(new TransactionTableModelSellerView(transactionList));
        transactionTable.updateUI();
         System.out.println("updateTables report");
        if(sellingList.size()==0)
        {
            saveTransactionListToFile();
            ShowMessageBox("All trees sold out. Click \"Confirm\" to exit");
            myAgent.doQuitAction("All trees sold out.");
        }

    }

    private void ShowMessageBox(String msg)
    {
        JOptionPane.showConfirmDialog(
                this, msg, "Notice",JOptionPane.DEFAULT_OPTION);
    }

    /**
     * save transaction list into a file.
     */
    private void saveTransactionListToFile() {
        Gson gson = new Gson();
        File transactionFile = new File("data/sellers/SellingList/" + myAgent.getLocalName() + "-TransactionList" + ".json");

        try {
            if (transactionFile.exists()) {
                //if the file already exist, delete it first.
                transactionFile.delete();
            }

            BufferedWriter bw = new BufferedWriter(new FileWriter(transactionFile));
            String json = gson.toJson(transactionList);
            bw.write(json);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
